function array2Tree(arr, pidFiled, pidValue, childFiled) {
  return arr.filter((item) => {
    item.children = arr.filter((curr) => {
      return curr[pidFiled] == item[childFiled];
    });
    return item[pidFiled] == pidValue;
  });
}

const flatArr = [
  { id: "01", pid: 0, name: "节点1" },
  { id: "0111", pid: "011", name: "节点1-1-1" },

  { id: "011", pid: "01", name: "节点1-1" },
  { id: "02", pid: 0, name: "节点2" },
  { id: "022", pid: "02", name: "节点2-2" },
  { id: "023", pid: "02", name: "节点2-3" },
  { id: "0222", pid: "022", name: "节点2-2-2" },
  { id: "03", pid: 0, name: "节点3" },
];

console.log(array2Tree(flatArr, "pid", 0, "id"));
